package com.pinkpig.excel.row;


import com.pinkpig.excel.metadata.ColumnMetaData;
import com.pinkpig.excel.metadata.TableMetaData;
import com.pinkpig.excel.row.model.ColumnRowModel;
import com.pinkpig.excel.row.model.RowModel;

import java.util.ArrayList;
import java.util.List;

public class ColumnRowModelBuilder implements RowModelBuilder<TableMetaData> {

    @Override
    public List<RowModel> build(TableMetaData tableMetaData){
        List<RowModel> rows = new ArrayList<>();
        tableMetaData.getColumns().forEach(e->{
            ColumnRowModel row = new ColumnRowModel();
            row.setColumnName(e.getColumnName());
            row.setColumnNameCh(e.getRemarks());
            row.setNullable(e.isNullAble()?"Y":"N");
            row.setColumnDefault(e.getColumnDefault());
            if(null!=e.getRemarks()){
                int index1 = e.getRemarks().indexOf("-");
                int index2 = e.getRemarks().indexOf(":");
                if(index1>0 && index2>index1){
                    row.setColumnNameCh(e.getRemarks().substring(0, index1));
                    row.setRemark(e.getRemarks().substring(index2+1));
                }
            }
            row.setDataType(e.getDataTypeName());
            rows.add(row);
        });
        return rows;
    }

}
